package com.admin.scroller.photoorvideo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.MediaMetadataRetriever;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.admin.scroller.photoorvideo.callback.OnVideoStartAndEndChangedListener;

public class VideoProgressView extends View {

    private String videoPath;
    private Bitmap[] bitmaps=new Bitmap[10];

    private int width;
    private int height;
    private int offsetX=50;//偏移量

    private int leftBar=0;
    private int rightBar;
    private int barWidth=offsetX;

    private long videoDuration;
    private long videoProgress;
    private long progress=offsetX;
    private long cupDuration;

    private Paint paintBar;
    private Paint paintFrame;
    private Paint paintProgress;

    private float startX;
    private int moveType;// 0 不移动  1  移动左侧  2 移动右侧

    private OnVideoStartAndEndChangedListener videoStartAndEndChangedListener;

    public VideoProgressView(Context context) {
        super(context);
        initView();
    }

    public VideoProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public VideoProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        paintBar=new Paint();
        paintFrame=new Paint();
        paintProgress=new Paint();
        initPoint(paintBar,"#eeb422",true);
        initPoint(paintFrame,"#eeb422",false);
        initPoint(paintProgress,"#32cd32",true);
    }

    private void initPoint(Paint paint,String color,boolean full){
        paint.setAntiAlias(true);
        if(full) {
            paint.setStyle(Paint.Style.FILL);
        }else {
            paint.setStyle(Paint.Style.STROKE);
        }
        paint.setColor(Color.parseColor(color));
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(TextUtils.isEmpty(videoPath)){
            return;
        }
        width=getWidth();
        height=getHeight();
        if(rightBar==0||rightBar==width){
            rightBar=width-offsetX;
        }
        if (leftBar==0){
            leftBar=offsetX;
        }

        int s=(width-offsetX*2)/10;

        //绘制bitmap
        RectF rectF;
        for(int i=0;i<10;i++){
            rectF=new RectF(i*s+offsetX,0,(i+1)*s+offsetX,height);
            canvas.drawBitmap(bitmaps[i],null,rectF,null);
        }

        //绘制左右的控制按钮
        RectF left=new RectF(leftBar-offsetX,0,leftBar,height);
        canvas.drawRect(left,paintBar);
        RectF right=new RectF(rightBar,0,rightBar+offsetX,height);
        canvas.drawRect(right,paintBar);
        RectF frame=new RectF(leftBar,0,rightBar,height);
        paintFrame.setStrokeWidth(3);
        canvas.drawRect(frame,paintFrame);

        //绘制播放进度位置
        if(progress<leftBar){
            progress=leftBar;
        }

        RectF p=new RectF(progress-5,0,progress+5,height);
        canvas.drawRect(p,paintProgress);

    }

    public void setVideoDuration(int duration){
        this.videoDuration=duration;
    }

    public void setVideoPath(String path){
        if(TextUtils.isEmpty(path)) return;
        videoPath=path;
        MediaMetadataRetriever retriever=new MediaMetadataRetriever();
        retriever.setDataSource(path);
        String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);//时长(毫秒
        videoDuration=Long.valueOf(duration);
        long s=videoDuration/9;
        for(int i=0;i<10;i++){
            bitmaps[i]=retriever.getFrameAtTime(i*s*1000);
        }
        retriever.release();
        invalidate();
    }

    public void setPaintProgress(Paint paintProgress) {
        this.paintProgress = paintProgress;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX=event.getX();
                if(Math.abs(startX-leftBar)<offsetX){
                    moveType =1;
                }else if(Math.abs(startX-rightBar)<offsetX){
                    moveType =2;
                }else {
                    moveType=0;
                }
                if(videoStartAndEndChangedListener!=null){
                    videoStartAndEndChangedListener.onStartChanged();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if(moveType!=0){
                    moveAction(event.getX());
                }
                break;
            case MotionEvent.ACTION_UP:
                if(videoStartAndEndChangedListener!=null){
                    long s=videoDuration*(leftBar-offsetX)/(width-offsetX*2);
                    long e=videoDuration*(rightBar-offsetX)/(width-offsetX*2);
                    videoStartAndEndChangedListener.onNewStartEndbounday((int)s,(int)e);
                }
                break;
        }
        return true;
    }

    private void moveAction(float x){
        float d=x-startX;
        Log.d("=========","********* " +d);
        if(moveType==1){
            if(leftBar+d<offsetX){
                leftBar=offsetX;
            }else{
                //移动左侧边界
                if(rightBar-(leftBar+d)>getMinTimeDis()){
                    leftBar+=d;
                }
            }
        }else {
            if(rightBar+d>width-offsetX){
                rightBar=width-offsetX;
            }else {
                //移动右侧边界
                if(rightBar+d-leftBar>getMinTimeDis()){
                    rightBar+=d;
                }
            }
        }
        startX=x;
        invalidate();
    }

    private int getMinTimeDis(){
        return (int)((width-offsetX*2)*5000/videoDuration);
    }

    public void setVideoStartAndEndChangedListener(OnVideoStartAndEndChangedListener videoStartAndEndChangedListener) {
        this.videoStartAndEndChangedListener = videoStartAndEndChangedListener;
    }

    public void setVideoProgress(long videoProgress) {
        this.videoProgress = videoProgress;
        progress=offsetX+(long)((width-offsetX*2)*videoProgress*1f/videoDuration);
        invalidate();
    }
}
